﻿@model SMECustomerWebsite.Models.ViewModels.CloseDao.CloseDaoRequestViewModel
<div class="col-md-12 col-sm-12 col-xs-12">
    <div class="x_panel">
        <div class="x_title">
            <h2><i class="fa fa-bars"></i> @SMECustomerWebsite.Core.Resources.Resource.AddnewByInput</h2>
            <ul class="nav navbar-right panel_toolbox">
                <li>
                    <a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                </li>
            </ul>
            <div class="clearfix"></div>
        </div>
        <div class="x_content">
            @if (Model.CurrentUser !=null && Request.Cookies[Model.CurrentUser.UserName + "CLDError"] != null)
            {
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    @Request.Cookies[Model.CurrentUser.UserName + "CLDError"].Value
                </div>
            }
            @Html.Partial("AddCloseDaoRequestByInput")
        </div>
    </div>
</div>

@section scripts
{
    <!-- validator -->
    <script src="~/Scripts/vendors/validator/validator.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            eraseCookie('@Model.CurrentUser.UserName' + 'CLDError');
            $('#position_name').select2({
                placeholder: 'Chọn vị trí'
            });
            $('#branch_register').select2({
                placeholder: 'Chọn trung tâm'
            });
        })

    </script>
    <!-- validator -->
    <script>
        // initialize the validator function
        validator.message.date = 'not a real date';
        $('form')
          .on('blur', 'input[required], input.optional, select.required', validator.checkField)
          .on('change', 'select.required', validator.checkField)
          .on('keypress', 'input[required][pattern]', validator.keypress);

        $('.multi.required').on('keyup blur', 'input', function () {
            validator.checkField.apply($(this).siblings().last()[0]);
        });

        $('form').submit(function (e) {
            e.preventDefault();
            var submit = true;

            // evaluate the form using generic validaing
            if (!validator.checkAll($(this))) {
                submit = false;
            }

            if (submit)
                this.submit();

            return false;
        });

        $("#daoSale").select2({
            ajax: {
                url: "@Url.Action("GetListAllDaoCode", "SaleCode")",
                dataType: 'json',
                type: "POST",
                delay: 250,
                data: function (params) {
                    return {
                        searchText: params.term, // search term
                        page: params.page
                    };
                },
                processResults: function (data, params) {
                    params.page = params.page || 1;
                    return {
                        results: data,
                        pagination: {
                        }
                    };
                },
                cache: true
            },
            placeholder: "Search By DAO",
            allowClear: true,
            escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
            minimumInputLength: 1,
            templateResult: formatRepo, // omitted for brevity, see the source of this page
            templateSelection: formatRepoSelection // omitted for brevity, see the source of this page,
        });

        $("#daoSale").val(null).trigger("change");
        function formatRepo(repo) {
            if (repo.loading) return repo.text;

            var markup = "<div class='select2-result-repository clearfix'>" +
              "<div class='select2-result-repository__meta'>" +
                "<div class='select2-result-repository__title'>" + repo.id + "</div>";

            markup += "<div class='select2-result-repository__description'>" + repo.daoName + "</div>";

            markup += "<div class='select2-result-repository__statistics'>" +
              "<div class='select2-result-repository__forks'>Mã nhân viên: " + repo.staffId + "</div>" +
            "</div>" +
            "</div></div>";

            return markup;
        }
        var initSelection = function (elem, cb) {
            console.log(elem)
            return elem
        }
        function formatRepoSelection(repo) {

            return repo.text;
        }
    </script>
    <!-- /validator -->
}
